﻿@using System.Web.Optimization
@using VocaDb.Model.Domain.Images
@using VocaDb.Web.Helpers;
@using VocaDb.Web.Models.Shared
@using Res = VocaDb.Web.Resources.Views.Tag.EditStrings;
@using EditRes = ViewRes.EntryEditStrings;
@inherits VocaDb.Web.Code.VocaDbPage<VocaDb.Web.Models.Tag.TagEditViewModel>

@{
	PageProperties.Title = "Edit tag - " + Model.CurrentName;
	ViewBag.Parents = new[] {
		Html.ActionLink("Tags", "Index"),
		Html.ActionLink(Model.CurrentName, "DetailsById", new { id = Model.Id, slug = Model.UrlSlug })
	};

	var thumbUrl = Url.EntryImageOld(Model.Thumb, ImageSize.SmallThumb);

	var validationErrors = new Dictionary<string, string> {
		{ "validationError_needDescription", Res.ValidationNeedDescription }
	};

}

@section BodyScripts {
@Scripts.Render("~/bundles/shared/edit", "~/bundles/Tag/Edit")
<script type="text/javascript">

	$(document).ready(function () {

		initPage();

		var urlMapper = new vdb.UrlMapper('@RootPath');
		var tagRepo = new vdb.repositories.TagRepository('@RootPath');
		var userRepo = new vdb.repositories.UserRepository(urlMapper);

		tagRepo.getById('@Model.Id', "AliasedTo,TranslatedDescription,Names,Parent,RelatedTags,WebLinks", '@UserContext.LanguagePreference.ToString()', function (contract) {
			var viewModel = new vdb.viewModels.tags.TagEditViewModel(urlMapper, userRepo, contract);
			ko.applyBindings(viewModel);
		});

	});

</script>
}

@section Toolbar {
@if (Model.CanDelete) {
	if (!Model.Deleted) {
		<a href="#" id="deleteLink" data-bind="click: deleteViewModel.show">@ViewRes.SharedStrings.Delete</a>
	} else if (Login.CanDeleteEntries) {
		@Html.ActionLink("Restore", "Restore", new { id = Model.Id }, new { id = "restoreLink" })
	}
	if (Login.CanMoveToTrash) {
		<a href="#" id="trashLink" data-bind="click: trashViewModel.show">@EditRes.MoveToTrash</a>
	}
}
@if (Login.CanMergeEntries) {
	<a href="@Url.Action("Merge", new { id = Model.Id })" data-bind="jqButton">@EditRes.Merge</a>
}
}

@KnockoutHelpers.EntryValidationMessage(Model.Draft, validationErrors, null)

<br />
<form action="@Url.Action("Edit", "Tag")" method="POST" enctype="multipart/form-data" data-bind="submit: submit">
	
	@Html.HiddenFor(m => m.Id)
	@Html.HiddenFor(m => m.Name)
	@Html.HiddenFor(m => m.UrlSlug)

	@Helpers.SaveAndBackBtn(Url.Action("DetailsById", new { id = Model.Id, slug = Model.UrlSlug }))
	
	<div class="editor-label">
		<label class="helpTip" data-bind="qTip">
			Default language
		</label>
	</div>
	<div class="editor-field">
		@KnockoutHelpers.LanguageSelectionDropDown("defaultNameLanguage")
		@Html.HiddenForKnockout(m => m.DefaultNameLanguage, "value: defaultNameLanguage()")
	</div>

	<div class="editor-label">
		<label class="helpTip" data-bind="qTip: null">
			Names
		</label>
		@Helpers.RequiredField()
	</div>
	<div class="editor-field" data-bind="with: names">
		@{ Html.RenderPartial("KnockoutPartials/_NamesEditor", new NamesEditorViewModel()); }
		@Html.ValidationMessageFor(m => m.Names)
		@Html.HiddenForKnockout(m => m.Names, "value: ko.toJSON($data.toContracts())")
	</div>

	<div class="editor-label">
		@Html.LabelFor(m => m.CategoryName)
	</div>
	<div class="editor-field">
		@Html.TextBoxForKnockout(m => m.CategoryName, "textInput: categoryName, tagCategoryAutoComplete: categoryName", maxLength: 30)
		@Html.ValidationMessageFor(m => m.CategoryName)
	</div>

	<div class="editor-label">
		<label for="parent" class="helpTip" title="Parent tag groups related tags under one parent. Child tags are still considered separate.">Parent</label>
	</div>
	<div class="editor-field">
		@KnockoutHelpers.LockingAutoComplete("tagAutoComplete", "parent", "parentName", extraBindings: "tagFilter: denySelf, clearValue: true, allowAliases: false")
		@Html.HiddenForKnockout(m => m.Parent, "value: ko.toJSON(parent)")
	</div>

	<div class="editor-label">
		@Html.LabelFor(m => m.Description)
		@Helpers.MarkdownNotice()
	</div>
	<div class="editor-field entry-edit-description" data-bind="with: description">
		@{ Html.RenderPartial("Partials/_EnglishTranslatedStringEdit"); }
		@Html.HiddenForKnockout(m => m.Description, "value: ko.toJSON($data)")
	</div>

	<div class="editor-label">
		Thumbnail
	</div>
	<div class="editor-field">
		<div class="media">
			@if (!string.IsNullOrEmpty(thumbUrl)) {
				<img class="pull-left media-object" src="@thumbUrl" alt="Thumb" />
			}
			<div class="media-body">
				@Helpers.ImageUploadMessage()
				<input type="file" id="thumbPicUpload" name="thumbPicUpload" />
			</div>
		</div>
	</div>
	
	<div class="editor-field withMargin" title="Hides this tag from suggested tags for albums and artists">
		@Html.CheckBoxFor(m => m.HideFromSuggestions)
		Hide from suggestions
	</div>

	<div class="editor-label withMargin">
		@Res.RelatedTags
	</div>
	<div class="editor-field">
		<table>
			<tbody data-bind="foreach: relatedTags">
				<tr>
					<td>
						<a data-bind="attr: { href: vdb.utils.EntryUrlMapper.details_tag(id, urlSlug) }, text: name"></a>
					</td>
					<td>
						<a data-bind="click: function () { $parent.relatedTags.remove($data); }" href="#" class="textLink removeLink">@ViewRes.SharedStrings.Remove</a>
					</td>
				</tr>
			</tbody>
		</table>

		<input type="text" data-bind="tagAutoComplete: addRelatedTag, tagFilter: allowRelatedTag" maxlength="128" placeholder="@ViewRes.SharedStrings.Search" class="input-xlarge" />
		@Html.HiddenForKnockout(m => m.RelatedTags, "value: ko.toJSON(relatedTags)")
	</div>
	
	<div class="editor-label">
		Valid for
	</div>
	<div class="editor-field">
		@foreach (var entryType in Model.AllTagTargetTypes) {
			<input type="checkbox" data-bind="checked: hasTargetType(@((int)entryType.Key))" />
			@entryType.Value
			<br />
		}
		@Html.HiddenForKnockout(m => m.Targets, "value: targets")
	</div>
	
	<div class="editor-label">
		<label class="helpTip" data-bind="qTip: null" title="@EditRes.ExternalLinksQuickHelp">
			@EditRes.WebLinks
		</label>
	</div>
	<div class="editor-field" data-bind="with: webLinks">
		@KnockoutHelpers.WebLinksEditViewKnockout(false)
		@Html.HiddenForKnockout(m => m.WebLinks, "value: ko.toJSON(webLinks)")
	</div>

	<div class="editor-label">
		@Helpers.HelpLabel(EditRes.Status, Resources.CommonMessages.EntryStatusExplanation)
	</div>
	<div class="editor-field">
		@Html.EnumDropDownListFor(m => m.Status, Translate.EntryStatusNames.GetValuesAndNames(Model.AllowedEntryStatuses))
	</div>
	
	<br />
	<p>@EditRes.UpdateNotes</p>
	<textarea name="UpdateNotes" id="UpdateNotes" class="input-xxlarge" rows="4" maxlength="200"></textarea>

	<br />
	@Helpers.SaveAndBackBtn(Url.Action("DetailsById", new { id = Model.Id, slug = Model.UrlSlug }))

</form>

@EntryDetailsHelpers.EntryDeletePopup(EditRes.ConfirmDelete)
@EntryDetailsHelpers.EntryTrashPopup(EditRes.ConfirmMoveToTrash)
